<template>
  <div class="industryBody">
    <el-table
      :data="tableData"
      :row-class-name="tableRowClassName"
      height="400px"
      style="width:100%;">
      <!-- 国内技术相似 -->
      <template v-if="dataType==='cnCompanySort'">
        <el-table-column
          prop="companyName"
          label="名称">
          <template slot-scope="scope">
            <p class="name" @click="goDetail(scope.row.companyName)">{{scope.row.companyName}}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="similarityNum"
          sortable
          label="相似度"
          width="100">
        </el-table-column>
        <el-table-column
          prop="companyPatentNum2"
          sortable
          label="分数"
          width="100">
        </el-table-column>
      </template>
      <!-- 国外技术相似 -->
      <template v-else>
        <el-table-column
          prop="companyName"
          label="名称">
          <template slot-scope="scope">
            <p class="name"  @click="goDetail(scope.row.companyName)">{{scope.row.companyName}}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="companyPatentNum2"
          sortable
          label="分数"
          align="center">
        </el-table-column>
      </template>
      <el-table-column
        label="操作"
        align="center"
        width="77">
        <template slot-scope="scope">
          <span :title="scope.row.isOpponent ? '取消对比' : '添加对比'" class="iconfont contrast icon-jia" :class="{'active':(scope.row.isOpponent)}" @click="contrast(scope, 'tableData')"></span>
        </template>
      </el-table-column>
    </el-table>
  </div>
  
</template>

<script>
export default {
  props: {
    dataType: {
      type: String
    },
    companyName: {
      type: String
    }
  },
  data: () => ({
    tableData: [],
    getData: [false, false],
    cnData: [], // 计算最大值
    enData: [],
    max: ''
  }),
  methods: {
    goDetail(name){
            this.$Bus.$emit("progressBar", name)
        },
    getMax() {
      let arrRank = [...this.cnData, ...this.enData]
      let arrRanks = []
      arrRank.forEach(v=>{
        arrRanks.push(v.companyPatentNum)
      })
      this.max = Math.max.apply(null,arrRanks)
      this.renderTable()
    },
    renderTable() {
      if (this.dataType === 'cnCompanySort') {
        let arrRank = this.cnData
        arrRank.forEach(v=>{
          // 分数
          if(v.companyScore){
            v.companyPatentNum2 = v.companyScore
          }else{
            v.companyPatentNum2 = Math.floor((v.companyPatentNum/this.max) * 100) / 100
          }
          // 相似度
          let arr = v.similarity.toString().split('.')
          if(arr[1]&&arr[1].length>3){
            v.similarityNum = Math.floor(v.similarity * 100) / 100 // v.similarity.toFixed(3)
          }else{
            v.similarityNum = v.similarity
          }
        })
        // 获取排名（按分数升序排列）
        let b = arrRank.sort((a,b) => {
            return Number(b.companyPatentNum2)-Number(a.companyPatentNum2)
        })
        this.tableData = b

      } else if (this.dataType === 'enCompanySort') {
        let arrRank = this.enData
        arrRank.forEach(v=>{
          // 分数
          if(v.companyScore){
            v.companyPatentNum2 = v.companyScore
          }else{
            v.companyPatentNum2 = Math.floor((v.companyPatentNum/this.max) * 100) / 100
          }
        })
        // 获取排名（按分数升序排列）
        let b = arrRank.sort((a,b) => {
            return Number(b.companyPatentNum2)-Number(a.companyPatentNum2)
        })
        this.tableData = b
      }
      let companyNames = []
      for (let i in this.tableData) {
        companyNames.push(this.tableData[i].companyName)
      }
      this.getKcOpponent(companyNames, 'tableData')
    },
    // 对比
    contrast(scope, data) {
      let row = scope.row
      let companyName = row.companyName
      let curData = this[data]
      if (!row.eid) {
        let promise = new Promise((resolve, reject) => {
          this.$post(this.$Url.search.companyList, {
            name: companyName,
            ipi: 1,
            ips: 1
          }).then(res => {
            let data = res.data
            if (data) {
              let eid = data.result && data.result[0]._id
              this.$set(curData[scope.$index], 'eid', eid)
              resolve(eid)
            }
          }).catch(err => {})
        })
        promise.then((eid) => {
          if (!row.isOpponent) { // 添加竞争对手对比
            this.$post(this.$Url.company.opponentAdd, {
              companyId: eid,
              companyName: companyName
            }).then(res => {
              if(res.status == 200){
                this.$message({
                  type: 'success',
                  message: '添加成功',
                  duration: 1000
                })
                this.$set(this[data][scope.$index], 'isOpponent', true)
              }else{
                this.$message({
                  type: 'error',
                  message: '添加失败',
                  duration: 1000
                })
              }
            }).catch(err => {})
          } else { // 取消竞争对手对比
            this.$delete(`${this.$Url.company.opponenDelete}/${eid}`).then(res => {
              if(res.status == 200){
                this.$message({
                  type: 'success',
                  message: '取消成功'
                })
                this.$set(this[data][scope.$index], 'isOpponent', false)
              }else{
                this.$message({
                  type: 'error',
                  message: '取消失败',
                  duration: 1000
                })
              }
            }).catch(err => {})
          }
        })
      } else {
        if (!row.isOpponent) { // 添加竞争对手对比
          this.$post(this.$Url.company.opponentAdd, {
            companyId: row.eid,
            companyName: companyName
          }).then(res => {
            if(res.status == 200){
              this.$message({
                type: 'success',
                message: '添加成功',
                duration: 1000
              })
              this.$set(this[data][scope.$index], 'isOpponent', true)
            }else{
              this.$message({
                type: 'error',
                message: '添加失败',
                duration: 1000
              })
            }
          }).catch(err => {})
        } else { // 取消竞争对手对比
          this.$delete(`${this.$Url.company.opponenDelete}/${row.eid}`).then(res => {
            if(res.status == 200){
              this.$message({
                type: 'success',
                message: '取消成功'
              })
              this.$set(this[data][scope.$index], 'isOpponent', false)
            }else{
              this.$message({
                type: 'error',
                message: '取消失败',
                duration: 1000
              })
            }
          }).catch(err => {})
        }
      }
    },
    // 批量查询竞争对手
    getKcOpponent(companyNames, dataName) {
      this.$post(this.$Url.evaluation.kcOpponent, companyNames).then(res => {
        let data = res.data
        if (data.length > 0) {
          for (let i in data) {
            f: for (let j in this[dataName]) {
              if (data[i] === this[dataName][j].companyName) {
                this.$set(this[dataName][j], 'isOpponent', true)
                break f
              }
            }
          }
        }
      }).catch(err => {})
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2) {
        return 'warning-row'
      } 
      return ''
    },
  },
  watch: {
    getData() {
      if(this.getData.indexOf(false)<0){
        this.getMax()
      }
    },
  },
  mounted() {
    this.$Bus.$on('cnList', (data) => {
      this.cnData = data.cnCompanySort
      this.$set(this.getData, 0, true)
    })
    this.$Bus.$on('enList', (data) => {
      this.enData = data.enCompanySort
      this.$set(this.getData, 1, true)
    })
  }
}
</script>

<style lang="scss" scoped>
.vbtn {
  width: 85%;
  min-width: 85%;
  height: 30px;
  border-radius: 4px;
  font-size: 12px;
  color: $mainColor;
  background-color: $hoverColor !important;
}
.vbtn:hover {
  font-weight: 600;
  color: #fff;
  background-color: $mainColor !important;
}
.contrast {
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 24px;
  border-radius: 50%;
  text-align: center;
  font-size: 16px;
  color: #fff;
  background-color: #909399;
  cursor: pointer;
  &.active {
    background-color: orange;
  }
}
.name {
  font-weight: bold;
}
</style>


